#!/bin/sh
#
# Xsetup:	Setup an xlogin's display
#
# Copyright (c) 1998-2000 SuSE GmbH Nuernberg, Germany.
# please send bugfixes or comments to feedback@suse.de.
#
# Author: Werner Fink,   <werner@suse.de>
#
 LIBDIR=/usr/X11R6/lib/X11
 ETCDIR=/etc/X11
 XDMDIR=${ETCDIR}/xdm
 BINDIR=/usr/X11R6/bin

no_exit_on_failed_exec=1
type shopt &> /dev/null && shopt -s execfail
set +e     &> /dev/null

#
# Background picture:
# Store your preferred picture in XPM format (see xv(1) or ppm(5))
# You can run gzip on the file BackGround.xpm to save disk space.
#
 background=${XDMDIR}/BackGround.xpm

#
# Special screensaver parameters for xset, see manual page xset(1x).
# Default is no special parameters.  Use DPMS (VESA Power Savings
# Protocol) if USEDPMS is set to "yes".
#
 SAVESC=""
 USEDPMS="no"

#
# Default settings
#
# Note: If you need a special key mapping copy Xmodmap.remote to
# Xmodmap.<full_qualified_domain_name_of_the_terminal> where the
# suffix is the full qualified name identical with  the value of
# the DISPLAY variable without `:0', `:1' ... e.g.
# Xmodmap.xterminal.uni-yx.de

 sysmodmap=${ETCDIR}/Xmodmap
 defmodmap=${ETCDIR}/Xmodmap.remote
hostmodmap=${ETCDIR}/Xmodmap.${DISPLAY%:*}
      xset=${BINDIR}/xset
   xmodmap=${BINDIR}/xmodmap
   xpmroot=${BINDIR}/xpmroot
  xsetroot=${BINDIR}/xsetroot
  kde1root=/opt/kde/bin
  kde2root=/opt/kde2/bin
  kde3root=/opt/kde3/bin
  xconsole=${BINDIR}/xconsole
  xmessage=${BINDIR}/xmessage
      xrdb=${BINDIR}/xrdb
   backprg=${XDMDIR}/BackGround

#
# Check if kdm is running and does its own background
#
kdmpid=/var/run/xdm.pid
for p in ${kde1root}/kdm \
         ${kde2root}/kdm \
         ${kde3root}/kdm
do
    test -x $p || continue
    /sbin/checkproc -p $kdmpid $p 2>/dev/null || continue
    test -x ${p%/*}/kdmdesktop || continue
    backprg=${p%/*}/kdmdesktop
    $xrdb -override -retain <<-EOF
	#ifdef COLOR
	*Foreground: black
	*Background: #cdd2b4
	#endif
	#if   (WIDTH < 320)
	XConsole*geometry:	125x80+0-0
	#elif (WIDTH < 400)
	XConsole*geometry:	130x85+0-0
	#elif (WIDTH < 640)
	XConsole*geometry:	150x90+0-0
	#elif (WIDTH < 800)
	XConsole*geometry:	240x95+0-0
	#elif (WIDTH < 1024)
	XConsole*geometry:	300x100+0-0
	#elif (WIDTH < 1152)
	XConsole*geometry:	384x110+0-0
	#elif (WIDTH < 1280)
	XConsole*geometry:	432x120+0-0
	#else
	XConsole*geometry:	480x130+0-0
	#endif
	EOF
    break;
done

#
# Check if gdm is running and if so set the Xresources
#
pidof -s gdm > /dev/null 2>&1
if test $? -eq 0 ; then
    $xrdb -override -retain ${XDMDIR}/Xresources
fi

#
# Screensaver
#
if test -x $xset ; then
    $xset s on
    test -n "$SAVESC"       && $xset s $SAVESC
    test "$USEDPMS" = "yes" && $xset +dpms
fi

#
# Set keymap for various displays
#
if test -x $xmodmap ; then
    if test -z "${DISPLAY%:*}" ; then
	# local connection
	test -r $sysmodmap  && $xmodmap $sysmodmap
    else
	# TCP/IP connection (remote or local)
	test -r $defmodmap  && $xmodmap $defmodmap
	test -r $hostmodmap && $xmodmap $hostmodmap
    fi
fi

#
# Handle background:
#   First users choise, if no choise is given
#   use the system defaults.
#
if   test -s ${background}.gz -a -x $xpmroot ; then
    $xpmroot ${background}.gz
elif test -s ${background}    -a -x $xpmroot ; then
    $xpmroot $background
elif test -x $backprg ; then
    $backprg
else
    $xsetroot -gray
fi

#
# Only for display :0 we have to set up the xconsole to be
# sure that the system messages are visible.
#
case "$DISPLAY" in
    :0|:0.0)	;;
    *)	exit 0	;;
esac

#
# Do not start xconsole for kdm
#
/sbin/checkproc -p /var/run/kdm.pid /opt/kde3/bin/kdm  && exit 0

#
# The geometry of xconsole is set in the Xresource file.
#
(
    exec setsid $xconsole -notify -nostdin -verbose -exitOnFail
) & echo $! > /var/run/xconsole.pid

exit 0
